/*! \file logparser.h
    \brief This file contains declaration of class ARSARSLogParser.
    \version 1.0
*/

#ifndef __LOG_PARSER_H
#define __LOG_PARSER_H

#include <QHash>
#include <QRegExp>
#include <QTextStream>
#include <QDomDocument>

#include <dynamicmodel.h>
#include <adapterinterface.h>

extern QHash<int, QRegExp> g_LogStrMap;

/*! \class LogFileAdapter
    \brief This class is used to convert ARS log to DynamicModel.
*/
class LogFileAdapter : public QObject,
                       public DynamicModelAdapterPluginInterface
{
    Q_OBJECT
    Q_INTERFACES(PluginInterface DynamicModelAdapterPluginInterface)

public:

    /*! \enum LogStrKeys
        \brief Mapping for parser keys.
    */
    enum LogStrKeys
    {
        WFLG =                                    0,
        ACTL =                                    1,
        FLTR =                                    2,
        ACTL_PACKAGE_START =                      3,
        ACTL_PACKAGE_END =                        4,
        ACTL_PACKAGE_SCHEMA =                     5,
        ACTL_PACKAGE_SCREEN_TYPE =                6,
        FLTR_PACKAGE_START =                      7,
        FLTR_PACKAGE_RESTART =                    8,
        FLTR_PACKAGE_END =                        9,
        FLTR_PACKAGE_SCHEMA =                     10,
        FLTR_PACKAGE_SEPARATOR =                  11,
        PHASE =                                   12,
        PACKAGE_EVENT =                           13,
        ACTL_GUIDE_START =                        14,
        ACTL_GUIDE_END =                          15,
        FLTR_GUIDE_END =                          16,
        ACTL_EXECUTION =                          17,
        FLTR_EXECUTION =                          18,
        ACTL_Q_PASSED =                           19,
        ACTL_Q_FAILED =                           20,
        FLTR_Q_PASSED =                           21,
        FLTR_Q_FAILED =                           22,
        FLTR_DISABLED =                           23,
        EXEC_ORDER =                              24,
        ACTL_ACT_PATTERN =                        25,
        FLTR_ACT_PATTERN =                        26,
        // Actions
        CALL_GUIDE =                              27,
        CLOSE_WINDOW =                            28,
        COMMIT_CHANGES =                          29,
        DDE =                                     30,
        DIRECT_SQL =                              31,
        GOTO_GUIDE_LABEL =                        32,
        MESSAGE =                                 33,
        FLTR_MESSAGE =                            34,
        OPEN_DIALOG =                             35,
        RUN_MACRO =                               36,
        ACTL_RUN_PROCESS =                        37,
        FLTR_RUN_PROCESS =                        38,
        ACTL_SET_FIELDS =                         39,
        FLTR_SET_FIELDS =                         40,
        CHANGE_FIELDS =                           41,
        PUSH_FIELDS =                             42,
        NOTIFY =                                  43,
        EXIT_GUIDE =                              44,
        LOG_TO_FILE =                             45,
        WAIT =                                    46,
        OLE =                                     47,
        GOTO =                                    48,
        SERVICE =                                 49,
        // Actions end
        ACTL_CALL_GUIDE_GUIDE =                   50,
        CHANGE_FIELDS_FIELD =                     51,
        CHANGE_FIELDS_ATTR =                      52,
        FIELD_ASSIGN =                            53,
        EVENT_ON_CONTROL =                        54,
        EVENT_ON_WINDOW_OPEN =                    55,
        EVENT_ON_SET_DEFAULT =                    56,
        EVENT_ON_GAIN_FOCUS =                     57,
        EVENT_ON_LOADED =                         58,
        EVENT_GET =                               59,
        EVENT_ON_MENU_CHOICE =                    60,
        EVENT_ON_RETURN =                         61,
        EVENT_ON_SUBMIT =                         62,
        EVENT_CREATE =                            63,
        EVENT_SET =                               64,
        EVENT_DELETE =                            65,
        EVENT_ON_AFTER_SUBMIT =                   66,
        EVENT_ON_QUERY =                          67,
        EVENT_ON_DISPLAY =                        68,
        EVENT_ON_WINDOW_CLOSE =                   69,
        EVENT_ON_MODIFY =                         70,
        EVENT_ON_UN_DISPLAY =                     71,
        ACTL_SCREEN_TYPE =                        72,
        SCREEN_TYPE_QUERY =                       73,
        SCREEN_TYPE_CREATE =                      74,
        SCREEN_TYPE_DIALOG =                      75,
        SCREEN_TYPE_SET =                         76,
        LINKED_TO_FIELD =                         77,
        TID =                                     78,
        RPC_ID =                                  79,
        QUEUE =                                   80,
        CLIENT_RPC =                              81,
        USER =                                    82,
        TIMESTAMP =                               83,
        TIMESTAMPSTART =                          84,
        TIMESTAMPEND =                            85,
        FLTR_NAME =                               86,
        FLTR_ACTION_ADJOURNED =                   87,
        LINE_API =                                88,
        LINE_SQL =                                89,
        CHANGE_FILED_READONLY =                   90,
        CHANGE_FILED_READWRITE =                  91,
        CHANGE_FILED_DISABLED =                   92,
        CHANGE_FILED_VISIBLE =                    93,
        CHANGE_FILED_HIDDEN =                     94,
        CHANGE_FILED_FONT =                       95,
        CHANGE_FILED_FONT_EDIT_FIELD =            96,
        CHANGE_FILED_FONT_OPTIONAL_FIELD =        97,
        CHANGE_FILED_FONT_PUSH_BUTTON =           98,
        CHANGE_FILED_FONT_SYSTEM_FIELD =          99,
        CHANGE_FILED_FONT_RADIO_BUTTON =          100,
        CHANGE_FILED_FONT_REAUIRED_FIELD =        101,
        CHANGE_FILED_FONT_HEADER1 =               102,
        CHANGE_FILED_FONT_HEADER2 =               103,
        CHANGE_FILED_FONT_HEADER3 =               104,
        CHANGE_FILED_FONT_NOTE_TEXT =             105,
        CHANGE_FILED_FONT_DETAIL_TEXT =           106,
        CHANGE_FILED_LABEL_COLOR =                107,
        CHANGE_FILED_LABEL_COLOR_DEFAULT =        108,
        CHANGE_FILED_LABEL_COLOR_CUSTOM =         109,
        CHANGE_FILED_LABEL =                      110,
        DDE_SERVICE =                             111,
        DDE_TOPIC =                               112,
        DDE_ACTION =                              113,
        DDE_ACTION_EXECUTE =                      114,
        DDE_ACTION_POKE =                         115,
        DDE_PATH =                                116,
        DDE_COMMAND =                             117,
        DDE_ITEM =                                118,
        LOG_ROOT =                                119,
        MONTH_JAN =                               120,
        MONTH_FEB =                               121,
        MONTH_MAR =                               122,
        MONTH_APR =                               123,
        MONTH_MAY =                               124,
        MONTH_JUN =                               125,
        MONTH_JUL =                               126,
        MONTH_AUG =                               127,
        MONTH_SEP =                               128,
        MONTH_OCT =                               129,
        MONTH_NOV =                               130,
        MONTH_DEC =                               131,
        LOG_CREATE_DATE =                         132,
        USER_VERSION =                            133,
        USER_VERSION_VERSION =                    134,
        DIRECT_SQL_SQL =                          135,
        DIRECT_SQL_SERVER =                       136,
        ACTL_GUIDE_LABEL =                        137,
        FLTR_GUIDE_LABEL =                        138,
        REQUEST_ID_NULL =                         139,
        FLTR_GOTO_EXECUTION_ORDER =               140,
        FLTR_SKIPPED =                            141,
        FLTR_GOTO_EXECUTION_ORDER_ORDER =         142,
        ACTL_GOTO_EXECUTION_ORDER =               143,
        ACTL_GOTO_EXECUTION_ORDER_ORDER =         144,
        LINE_CLAT =                               145,
        FIELD_ID =                                146
    };

    enum LineType
    {
        LineUnknown,
        LineActiveLink,
        LineFilter,
        LineApi,
        LineSql,
        LineClat
    };

    /*! \todo remove */
    static const int ACTL_FIELD_ASSIGN_REMOVE_LEN = 19;

    /*! \todo remove */
    static const int FLTR_FIELD_ASSIGN_REMOVE_LEN = 158;

    /*! \enum ObjectsManagment
        Values for log objects.
    */
    enum ObjectsManagment
    {
        WorkflowRoot,
        WorkflowBlockBegin,
        WorkflowBlockEnd,
        WorkflowGuideBegin,
        WorkflowGuideEnd,
        WorkflowObjectBegin,
        WorkflowActionBegin,
        WorkflowFieldAssign,
        ObjectLineSql,
        ObjectLineApi,
        ObjectLineClat,
        Unknown
    };

    /*! Constructor. */
    LogFileAdapter(QObject * parent = 0);

    QString name() const;
    QString description() const;

    /*! Creates execution model and objects index from \a file and returns root item. */
    DynamicModel * createModel(QIODevice * source);

    bool loadSettings();

    /*! \todo remove. */
    static void printItem( ARSExecItem * item, int level );


private:
    static QDomElement item2xml( const ARSExecItem * item, QDomDocument * document );
    static ARSExecItem * element2item( ObjectsIndex * objectsIndex, const QDomElement & element, ARSExecItem * parent = 0 );
    static QVariant string2Property( ObjectsIndex * objectsIndex, const QString & propertyString,
                                     const QMetaProperty & itemMetaProperty, ARSExecItem * item );
    static ObjectsManagment itemMark( const QString & line );

    static QString parseDateTime( ARSExecItem * item, const QString & line );
    static bool parseModelRoot( ARSExecItem * item, const QString & line );
    static bool parseALWorkflowBlockLine( ObjectsIndex * objectsIndex, ARSExecActlBlock * item, const QString & line );
    static bool parseFltrWorkflowBlockLine( ObjectsIndex * objectsIndex, ARSExecFltrBlock * item, const QString & line );
    static bool parseWorkflowGuideLine( ObjectsIndex * objectsIndex, ARSExecGuide * item, const QString & line,
                                 ARSExecWorkflowObject::ObjectType arsObjectType );
    static bool parseALWorkflowObjectLine( ObjectsIndex * objectsIndex, ARSExecActl * item, const QString & line );
    static bool parseFltrWorkflowObjectLine( ObjectsIndex * objectsIndex, ARSExecFltr * item, const QString & line );

    static bool parseActionLine( ObjectsIndex * objectsIndex, ARSExecAction * item, const QString & line,
                          ARSExecWorkflowObject::ObjectType arsObjectType );
    static bool parseALActCallGuideLine( ObjectsIndex * objectsIndex, ARSExecActionCallGuide * item, const QString & line );
    static bool parseALActGoToGuideLabelLine( ARSExecActionGoToGuideLabel * item, const QString & line );
    static bool parseFltrActGoToGuideLabelLine( ARSExecActionGoToGuideLabel * item, const QString & line );
    static bool parseFltrActCallGuideLine( ObjectsIndex * objectsIndex, ARSExecActionCallGuide * item, const QString & line );
    static bool parseALActChangeFieldsLine( ObjectsIndex * objectsIndex, ARSExecActionChangeFields * item, const QString & line );
    static bool parseALActDDELine( ARSExecActionDDE * item, const QString & line );
    static bool parseFltrGoToExecOrderLine( ARSExecActionGoto * item, const QString & line,
                                            ARSExecWorkflowObject::ObjectType arsObjectType );
    static bool parseActMessage( ARSExecActionMessage * item, const QString & line );
    static bool parseDirectSQLLine( ARSExecActionDirectSQL * item, const QString & line );
    static bool parseFieldAssign( ObjectsIndex * objectsIndex, ARSExecFieldAssign * item, const QString & line );
    static bool parseSQLLine( ARSExecSQLLine * item, const QString & line );
    static bool parseAPILine( ARSExecAPILine * item, const QString & line );
    static inline ARSExecWorkflowObject::ObjectType objectType( const QString & line );
    static inline ARSExecAction::ActionType actionType( const QString & line );
    static inline ARSExecWorkflowBlock::Event eventType( const QString & line );
    static inline ARSExecActlBlock::ScreenType screenType( const QString & line );
    static inline int serverPropertiesLen( const QString & line );
    static ARSExecItem * itemFromName( const QString & name, ARSExecItem * parent = 0 );
    static ARSExecAction * createAction( ARSExecAction::ActionType type, ARSExecItem * parent );
    static LineType lineType( const QString & line );
    static int createDateMonth( const QString & line );
};

#endif // __LOG_PARSER_H

////////////////////////////////////////////////////////////////////////////////////////////////////
